package com.shen.cas;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * ABA问题
 */
public class Demo02 {

    //CAS  compareAndSet  比较并交换
    public static void main(String[] args) {
        AtomicInteger atomicInteger = new AtomicInteger(2020);//设定初始值为2020

        //B线程
        System.out.println(atomicInteger.compareAndSet(2020, 2021));//true
        System.out.println(atomicInteger.get());//2021
        System.out.println(atomicInteger.compareAndSet(2021, 2020));//true
        System.out.println(atomicInteger.get());//2020

        //A线程
        System.out.println(atomicInteger.compareAndSet(2020, 2022));//false
        System.out.println(atomicInteger.get());//2022

    }
}
